package com.yq;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/111111")
public class servlet5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;text/plan;application/x-png;charset=utf-8");
        String servletPath = req.getServletPath();
        String realPath = getServletContext().getRealPath(servletPath);
        if ("/".equals(servletPath)){
            realPath=realPath+"\\index.html";
        }
        File file = new File(realPath);
        ServletOutputStream outputStream = resp.getOutputStream();
        if (file.exists()) {
            //文件存在，返回给客户端
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes,0,len);
            }
            fileInputStream.close();
        } else {
            resp.setStatus(404);
            outputStream.write("File not Found!!!!".getBytes());
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);

    }
}
